import config from './config'
import tool from './utils/tool'
import api from './api'
import http from './utils/request'
import { permission, rolePermission } from './utils/permission'
import auth from './directives/auth'
import role from './directives/role'
import time from './directives/time'
import copy from './directives/copy'
import errorHandler from './utils/errorHandler'
import * as elIcons from '@element-plus/icons-vue'
import * as scIcons from './assets/icons'
import {
	scTableColumn,
	scEcharts,
	scTable,
	scFileImport,
	scFileExport,
	scTableSelect,
	scFilterBar,
	scSelectPage,
	scUpload,
	scUploadMultiple,
	scUploadFile,
	scFormTable,
	scSelect,
	scForm,
	scPageHeader,
	scDialog,
	scTitle,
	scWaterMark,
	scQrCode,
	scStatusIndicator,
	scTrend,
	scSearch,
	scJsonEditor,
	scEchartsTotal
} from '/packages/index.js'

export default {
	install(app) {
		//挂载全局对象
		app.config.globalProperties.$CONFIG = config
		app.config.globalProperties.$TOOL = tool
		app.config.globalProperties.$HTTP = http
		app.config.globalProperties.$AUTH = permission
		app.config.globalProperties.$ROLE = rolePermission
		app.config.globalProperties.$API = api

		//注册全局组件

		app.component('scTable', scTable)
		app.component('scEcharts', scEcharts)
		app.component('scTableColumn', scTableColumn)
		app.component('scFilterBar', scFilterBar)
		app.component('scFileImport', scFileImport)
		app.component('scFileExport', scFileExport)
		app.component('scUpload', scUpload)
		app.component('scUploadMultiple', scUploadMultiple)
		app.component('scUploadFile', scUploadFile)
		app.component('scFormTable', scFormTable)
		app.component('scTableSelect', scTableSelect)
		app.component('scPageHeader', scPageHeader)
		app.component('scSelect', scSelect)
		app.component('scDialog', scDialog)
		app.component('scForm', scForm)
		app.component('scTitle', scTitle)
		app.component('scWaterMark', scWaterMark)
		app.component('scQrCode', scQrCode)
		app.component('scStatusIndicator', scStatusIndicator)
		app.component('scTrend', scTrend)
		app.component('scSelectPage', scSelectPage)
		app.component('scFilterBar', scFilterBar)
		app.component('scJsonEditor', scJsonEditor)
		app.component('scSearch', scSearch)
		app.component('scEchartsTotal', scEchartsTotal)

		//注册全局指令
		app.directive('auth', auth)
		app.directive('role', role)
		app.directive('time', time)
		app.directive('copy', copy)

		//统一注册el-icon图标
		for (let icon in elIcons) {
			app.component(`ElIcon${icon}`, elIcons[icon])
		}
		//统一注册sc-icon图标
		for (let icon in scIcons) {
			app.component(`ScIcon${icon}`, scIcons[icon])
		}

		//关闭async-validator全局控制台警告
		window.ASYNC_VALIDATOR_NO_WARNING = 1

		//全局代码错误捕捉
		app.config.errorHandler = errorHandler

		app.config.errorHandler = (err, instance, info) => {
			console.error('全局错误:', err)
			console.error('错误组件:', instance?.$options?.name || '未知组件')
			console.error('错误位置:', info)

			// 打印组件栈
			if (instance) {
				let current = instance
				while (current) {
					console.log('组件栈:', current.$options.name || '匿名组件')
					current = current.$parent
				}
			}
		}
	}
}
